// OSR b ServerRecv
package org.jvcs.fmwk;

import org.jvcs.fmwk.*;
import org.jvcs.common.*;
import org.jvcs.utils.*;

import java.net.*;
import java.io.*;

public class ServerTransfer extends Thread {
	public static int portNo = 0;
	ServerTransfer(int portNo) {
		this.portNo = portNo;
	}
    public void run() {
		try {
				ServerSocket ss = new ServerSocket(portNo);
				Socket s2 = ss.accept();
				try {
					//final String largeFile = "c:/temp/aa11.txt";
					String filePath = "c:/temp/TEST-CLIENT.txt";
					final String largeFile = filePath;
					FileInputStream fileInputStream = new FileInputStream(largeFile);
                    OutputStream socketOutputStream = s2.getOutputStream();
					long startTime = System.currentTimeMillis();
					byte[] buffer = new byte[G.BUFFER_SIZE];
                    int read;
					int readTotal = 0;
                    while ((read = fileInputStream.read(buffer)) != -1) {
                         socketOutputStream.write(buffer, 0, read);
                                        readTotal += read;
                                }
                                socketOutputStream.close();
                                fileInputStream.close();
                                
                                long endTime = System.currentTimeMillis();
                                System.out.println(readTotal + " bytes written in " + (endTime - startTime) + " ms.");
								ss.close();
								s2.close();
				} catch (Exception e) {
					System.out.println("Error: SEND FileTransfer Server: "+e.toString());
				}
				
			} catch (Exception e) {
				
				osr.pl("Issue in FileTransfer");
				
			}
		/*
		try {
			String java_cmd = "java -cp ";
			String jvcs_bin = System.getenv(G.JVCS_BIN);
			String jvcs_sa_program = "ServerFileTransfer";
			String filePath = "c:/temp/TEST-SERVER.txt";
			String command = java_cmd + jvcs_bin + " " + jvcs_sa_program + " "+ portNo + " "+filePath;
			Process child = Runtime.getRuntime().exec(command);
			child.waitFor();
			System.out.println(child.exitValue());


		} catch (Exception e) {
			osr.pl("Error in the run of FileTransfer Server"+e.toString());
		}
		*/
    }
}
